#include "Mesh.h"

CMesh::~CMesh()
{
	delete[] m_Tris;
}

int CMesh::Intersect(CRay& _ray, IsectData &_isecData)
{
	int interResult = -1;

	for (int k = 0; k < m_Triangles; k++)
	{		
		interResult = m_Tris[k].Intersect(_ray, _isecData);

		if (interResult > 0)
		{
			break;
		}
	}

	return interResult;
}

// Use this to get intersection point P in cartesian coords
int	CMesh::Intersect(CRay& _ray, IsectData &_isecData, CVector &_isecPoint)
{
	int interResult = -1;

	for (int k = 0; k < m_Triangles; k++)
	{		
		interResult = m_Tris[k].Intersect(_ray, _isecData);

		if (interResult > 0)
		{
			_isecPoint = 	((*m_Tris[k].GetA()) * _isecData.u) + 
							((*m_Tris[k].GetB()) * _isecData.v) +
							((*m_Tris[k].GetC()) * _isecData.t);
			break;
		}
	}

	return interResult;
}